#include "network.h"

CNetwork::CNetwork() : m_bIsRunning(false)
{
    m_socketServer = -1;
}

CNetwork::~CNetwork()
{
    
}

bool CNetwork::Init()
{
    return false;
}

bool CNetwork::Run()
{
    return false;
}

bool CNetwork::SendData(char* pData, int nLen)
{
    return true;
}

// 自定义trim函数
std::string CNetwork::trim(const std::string& str)
{
    size_t start = str.find_first_not_of(" \t\n\r");
    size_t end = str.find_last_not_of(" \t\n\r");
    
    if (start == std::string::npos || end == std::string::npos) {
        return "";
    }
    
    return str.substr(start, end - start + 1);
}

unsigned char CNetwork::CheckSumNet(char* szData, int nLen)
{
    char cResult = 0;
    for (int i = 0; i < nLen; i++)
    {
        cResult += szData[i];
    }
    return cResult;
}

std::string CNetwork::CheckSum(const std::vector<char>& listData)
{
    unsigned char cResult = 0;
    for (auto it = listData.begin(); it != listData.end(); it++)
    {
        cResult += (*it);
    }
    char buffer[3] = { 0x00, 0x00, 0x00 };
    snprintf(buffer, sizeof(buffer), "%02x", cResult);
    printf("CheckSum: %s\n", buffer);
    return buffer;
}